package com.amap.map.util.version;


import com.amap.map.R;
import com.amap.map.network.handler.CustomExecutorService;
import com.amap.map.network.handler.DataUploadHandler;
import com.amap.map.util.Configuration;
import com.amap.map.util.ToastUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class VersionUtils {
	
	// 调用更新的Activity 
    public Activity activity = null; 
    // 当前版本号 
    public int versionCode = 0; 
    // 当前版本名称 
    public String versionName = "";
    
    public VersionUtils( Activity activity ){
    	this.activity = activity ;
    	check() ;
    }
    
    /**
     * 
     * 检测更新
     */ 
    public void check() { 
        // 检测网络 
        if (isNetworkAvailable(this.activity) == false) { 
        	ToastUtil.show( activity.getApplicationContext() , "当前网络可用,请检查网络状态. " );
        	return ;
        } 
        if (true) {   // 如果网络可用,去服务器获取查询是否是最新版本
            try {
				PackageInfo info = activity.getPackageManager().getPackageInfo( activity.getPackageName(), 0 ); 
				this.versionCode = info.versionCode; 
				this.versionName = info.versionName; 
				//調用後台服務
				new VersionQueryHandler( versionCode ,versionName , new VersionUpdate(activity) ).run();
			} catch (NameNotFoundException e) {
			}
        } 
    } 
    
    /***
     * 判断网络是否开启
     * @param context
     * @return
     */
	   public static boolean isNetworkAvailable(Context context) { 
	        try { 
	            ConnectivityManager cm = (ConnectivityManager) context 
	            .getSystemService(Context.CONNECTIVITY_SERVICE); 
	            // 获取网络信息 
	            NetworkInfo info = cm.getActiveNetworkInfo(); 
	            // 返回检测的网络状态 
	            return (info != null && info.isConnected()); 
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	            return false; 
	        } 
	 
	    }

}
